// @flow
import React, { Component } from 'react'
import { AgGridReact } from 'ag-grid-react'
import styles from './CustomerGrid.css'
import CustomerActions from './CustomerActions'
import { TableResize } from 'ag-grid-addons'
// import Pagination from 'components/Pagination'
// import R from 'ramda'

type Props = {
  rowData: Array<Object>,
  refreshCustomers: Function,
  customersPermission: Object,
  history: Object,
  total: number,
  current: number,
  pageSize: number
}
type States = {
  columnDefs: Array<Object>
}
class CustomerGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  goToDevice: Function
  goToDeviceCustomer: Function
  api: Object
  TableResize: Function
  constructor (props: Props) {
    super(props)
    // const component = this
    this.state = {
      columnDefs: []
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      rowSelection: 'multiple',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
    this.goToDevice = this.goToDevice.bind(this)
    this.goToDeviceCustomer = this.goToDeviceCustomer.bind(this)
  }

  componentWillReceiveProps (nextProps: Props) {
    this.setState({
      columnDefs: [
        {
          headerName: '客户编号',
          field: 'customerNo',
          // checkboxSelection: true,
          maxWidth: 90,
          minWidth: 90,
          cellStyle: {
            'padding': '8px'
          }
        },
        {
          headerName: '客户名称',
          field: 'name',
          width: 300,
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '所属行业',
          field: 'industry',
          width: 150,
          cellStyle: {
            padding: '8px'
          }
        },
        // {
        //   headerName: '客户类型',
        //   field: 'type',
        //   width: 150,
        //   cellStyle: {
        //     padding: '8px'
        //   }
        // },
        {
          headerName: '所属地区',
          field: 'region.location',
          width: 150,
          cellStyle: {
            padding: '8px'
          }
        },
        // {
        //   headerName: '地址',
        //   width: 300,
        //   cellRenderer: (params) => {
        //     const { region } = params.data
        //     if (region) {
        //       const province = region.province || ''
        //       const city = region.city || ''
        //       const district = region.district || ''
        //       return province + city + district
        //     } else {
        //       return ''
        //     }
        //   },
        //   cellStyle: {
        //     padding: '8px'
        //   }
        // },
        {
          headerName: '权限',
          minWidth: 200,
          maxWidth: 200,
          cellRendererParams: {
            customersPermission: nextProps.customersPermission,
            refreshCustomers: nextProps.refreshCustomers
          },
          cellRendererFramework: CustomerActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'left'
          }
        }
      ]
    })
  }

  goToDevice (data: Object) {
    const { history } = this.props
    const location = {
      pathname: '/business/device-info',
      state: {
        customerId: data.id
      }
    }
    history.push(location)
  }
  goToDeviceCustomer (data: Object) {
    const { history } = this.props
    const location = {
      pathname: '/business/device-customer',
      state: {
        data: data
      }
    }
    history.push(location)
  }
  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }
  render () {
    const { columnDefs } = this.state
    const { rowData } = this.props
    return (
      <div className={styles['grid'] + ' ag-fresh'}>
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: columnDefs,
          rowData: rowData,
          onGridReady: this.onGridReady
        }}
          />
        {
          // <Pagination current={current} pageSize={pageSize} total={total} onChange={refreshCustomers} />
        }
      </div>
    )
  }
}

export default CustomerGrid
